package view.mt4j.util;

import org.mt4j.components.visibleComponents.shapes.AbstractShape;
import org.mt4j.components.visibleComponents.widgets.MTTextArea;
import org.mt4j.input.inputProcessors.IGestureEventListener;
import org.mt4j.input.inputProcessors.MTGestureEvent;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapEvent;
import org.mt4j.util.MTColor;

public abstract class ButtonListener implements IGestureEventListener {

	private final AbstractShape component;
	private final MTTextArea textArea;
	private final MTColor textColor;
	private final MTColor backgroundColor;
	private MTColor oldTextColor;
	private MTColor oldBackgroundColor;

	public ButtonListener(final AbstractShape component,
			final MTTextArea textArea, final MTColor textColor,
			final MTColor backgroundColor) {
		super();
		this.component = component;
		this.textArea = textArea;
		this.textColor = textColor;
		this.backgroundColor = backgroundColor;
	}

	@Override
	public boolean processGestureEvent(MTGestureEvent ge) {
		TapEvent te = (TapEvent) ge;
		switch (te.getId()) {
		case TapEvent.GESTURE_STARTED:
			oldTextColor = textArea.getFont().getFillColor();
			oldBackgroundColor = component.getFillColor();
			component.setFillColor(backgroundColor);
			textArea.setFontColor(textColor);
			break;
		case TapEvent.GESTURE_ENDED:
			component.setFillColor(oldBackgroundColor);
			textArea.setFontColor(oldTextColor);
			if (te.isTapped()) {
				ButtonListener.this.onClick();
			}
			break;
		default:
			break;
		}
		return false;
	}

	public abstract void onClick();

}
